package com.imooc.coupon.service.impl;

import com.google.common.collect.Maps;
import com.imooc.coupon.constant.CouponChannel;
import com.imooc.coupon.constant.CouponStatus;
import com.imooc.coupon.service.IStreamProducer;
import com.imooc.coupon.vo.CouponMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@Component
public class StreamProducerImpl implements IStreamProducer {

    @Autowired
    private Map<String, MessageChannel> channelMap;

    @Override
    public boolean sendMessage(String channelName, Object payload) {
        return sendMessage(channelName,payload, new MessageHeaders(Maps.newHashMap()));
    }

    @Override
    public boolean sendMessage(String channelName,Object payload, Map<String, Object> headers) {
        MessageChannel channel = channelMap.get(channelName);
        Message<Object> message = MessageBuilder.createMessage(payload, new MessageHeaders(headers));
        boolean result = channel.send(message);
        log.info(String.format("channel %s send message %s result %s",channelName,payload,result));
        return result;
    }
}
